import Vue from 'vue';
import { MY_NAME, MENU_MAP } from './constants';

// 防抖
export function _debounce(fn, delay) {
  var delay = delay || 200;
  let timer;
  return function () {
    const th = this;
    const args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      timer = null;
      fn.apply(th, args);
    }, delay);
  };
}
// 节流
export function _throttle(fn, interval) {
  let last;
  let timer;
  var interval = interval || 200;
  return function () {
    const th = this;
    const args = arguments;
    const now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(() => {
        last = now;
        fn.apply(th, args);
      }, interval);
    } else {
      last = now;
      fn.apply(th, args);
    }
  };
}

export const bus = new Vue();

export function hasName(list, item, isNum) {
  const { name } = item;
  const { type } = item;
  const tlist = list.filter((item) => item.type === type);
  const index = tlist.findIndex((item) => name === getPureFileName(item.name, !item.etag));
  return index >= 0;
}

export function getName(list, item, isNum) {
  const { name } = item;
  const { type } = item;
  if (!hasName(list, item, isNum)) { return name; }
  if (name.indexOf('_') < 0) {
    return [name, new Date().getTime()].join('_');
    // return isNum ? getName(list, { name:[name, 1].join('_'), true), type } : getName(list, { name:[name, 'copy'].join('_'), type });
  }
  const nameArray = name.split('_');
  const last = nameArray.pop();

  if (!isNaN(last)) {
    nameArray.push(new Date().getTime());
    return nameArray.join('_');
  }

  if (isNaN(last)) {
    nameArray.push(last, new Date().getTime());
    return nameArray.join('_');
  }

  /* if (isNum && !isNaN(last)) {
        nameArray.push(++last);
        return getName(list, {name: nameArray.join('_'), type}, true);
    }
    if (isNum && isNaN(last)) {
        nameArray.push(last, 1);
        return getName(list, {name: nameArray.join('_'), type}, true);
    }
    if (!isNum) {
        nameArray.push(last, 'copy');
        return getName(list, {name: nameArray.join('_'), type});
    } */
}

export const getFileType = (contentType) => {
  // 文本文档
  if (contentType.indexOf('text/plain') > -1) {
    return MY_NAME.TE;
  }
  // word
  if (contentType.indexOf('application/vnd.openxmlformats-officedocument.wordprocessingml.document') > -1) {
    return MY_NAME.WO;
  }
  // ppt
  if (contentType.indexOf('application/vnd.openxmlformats-officedocument.presentationml.presentation') > -1) {
    return MY_NAME.PP;
  }
  // excel
  if (contentType.indexOf('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') > -1) {
    return MY_NAME.EX;
  }
  // image
  if (contentType.indexOf('image') > -1) {
    return MY_NAME.PO;
  }
  // folder
  if (!contentType) {
    return MY_NAME.FO;
  }
  // unknown file
  return MY_NAME.TE;
};

export const getMenuIndexByPath = (menuPath) => {
  const menuKey = Object.entries(MENU_MAP).find(([key, val]) => {
    if (val === menuPath) {
      return true;
    }
    return false;
  })[0];
  return menuKey;
};

// 去除文件名中的后缀名以及文件路径slash
export const getPureFileName = (filename, isFolder) => {
  if (isFolder) {
    const splitnames = filename.split('/');
    return splitnames[splitnames.length - 2];
  }
  return filename.split('.')[0];
};
